package com.logic.landseaserver.task;

import java.util.Date;

import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.models.changeroom.service.ChangeRoomService;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.logic.landseaserver.models.payment.service.PaymentService;
import com.logic.landseaserver.service.IBillPaymentService;
import com.logic.landseaserver.service.IContractService;
import com.logic.landseaserver.service.IDepositService;
import com.logic.landseaserver.service.IScheduleService;
import com.logic.system.domain.Configuration;
import com.logic.system.service.IConfigurationService;
import com.logic.system.service.impl.ConfigurationService;

@Component
public class PaymentTask {

    private static final Logger log = LoggerFactory.getLogger(PaymentTask.class);
    @Autowired
    private IBillPaymentService iBillPaymentService;

    @Autowired
    private IDepositService depositService;

    @Autowired
    private IContractService contractService;
    @Autowired
    private IConfigurationService configService;

    @Autowired
    IConfigurationService configurationService;

    @Autowired
    private IScheduleService iScheduleService;
    
    /** 支付入口组件 **/
    @Autowired
    private PaymentService paymentService;

    @Autowired
    private ChangeRoomService changeRoomService;
    
    /**
     * 清除即将过期的预定以及支付账单信息
     */
    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void clearComeDuuPayInfo() {
        try {
            boolean cuteFlag = iScheduleService.cuteSchedule();
            if(cuteFlag){
                //Step1 : 同步支付中的预定和账单的支付状态
                log.info("Task for syn bill payment start");
                iBillPaymentService.updatePayStatus(null);
                log.info("Task for syn deposit payment start");
                depositService.updatePayStatus();
                log.info("Task for syn end");

                //Step2：清空预定
                //获取系统默认的预定失效时间
                String depositDate = configurationService.getStringValue(ConfigurationService.SYS_DEFAULT_DEPOSITCOMEDUETIME);
                Date depsitTime = DateUtils.addMinutes(new Date(), -Integer.parseInt(depositDate));

                log.info("Task for clearDeposit start");
                depositService.clearDeposit(depsitTime);
                log.info("Task for clearDeposit end");

                //Step3：清空账单支付记录
                //获取系统默认的预定失效时间
                String billDate = configurationService.getStringValue(ConfigurationService.SYS_DEFAULT_BILLCOMEDUETIME);
                Date billTime = DateUtils.addMinutes(new Date(), -Integer.parseInt(billDate));
                log.info("Task for clearBillPayment start");
                iBillPaymentService.clearBillPayment(billTime);
                log.info("Task for clearBillPayment end");
            }

        } catch (Exception e) {
            log.warn("clearComeDuuPayInfo is error:" + e.getMessage());
        }

    }

    /**
     * 定时删除合同
     */
    @Scheduled(fixedRate = 1000 * 60 * 20)
    public void deleteNoPayContracts() {
        try {
            Configuration config = configService.get("SYS_DELETE_CONTRACT_TIME");
            if (config != null) {
                Integer minutesMis = Integer.valueOf(config.getValue());
                log.warn("定时删除合同时间查为：" + minutesMis);
                boolean cuteFlag = iScheduleService.cuteSchedule();
                if(cuteFlag){
                    contractService.deleteContractList(minutesMis);
                }
            }
        } catch (Exception e) {
            log.warn("定时删除合同 error:" + e.getMessage());
        }
    }
    
    /**
     * 定时同步未完成的订单
     */
    @Scheduled(fixedRate = 1000 * 60 * 15)
    public void syncNoSuccessPaymentBill()
    {
        boolean cuteFlag = iScheduleService.cuteSchedule();
        if (cuteFlag) 
        {
            paymentService.syncBillPayment();
        }
    }


    /**
     * B合同已签约 A合同自动结束
     */
    @Scheduled(cron = "0 40 23 * * ?")
    public void updateContractEnd()
    {
        log.info("Task for updateContractEnd for  start.");
        // 开始执行业务
        try {
            String inDate =DateUtil.convertDate( DateUtils.addDays(new Date(),1));
            contractService.updateContractEnd(inDate);

        } catch (Exception e) {
            log.info("updateContractEnd Exception" + e.getMessage());
        }
        log.info("Task for updateContractEnd for end.");
        log.info("Task for Cancle Change Room start.");
        try {
            double overTimes = configurationService.getDoubleValue(ConfigurationService.CLREA_CHANGE_ROOM_TIME);
            
            int min = (int)overTimes * 60;
            
            String changeDate =DateUtil.convertDateTime(DateUtils.addMinutes(new Date(),-min)) ;
            changeRoomService.clearOverTimeCRRecord(changeDate);
        }catch (Exception ex){
            log.error("clearOverTimeCRRecord Exception" + ex.getMessage());
        }
        log.info("Task for Cancle Change Room end.");
    }
}